package com.eascs.admin.o2o.recommend.dao;

import java.util.GregorianCalendar;

import org.springframework.stereotype.Repository;

import com.eascs.admin.common.dao.BaseO2oDao;
import com.eascs.admin.common.utils.StringUtils;
import com.eascs.admin.o2o.internal.util.BaseUtils;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.recommend.vo.O2oRecommendVo;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.DateUtils;
import com.eascs.common.util.StringUtil;
import com.eascs.web.sso.UserHolder;

@Repository
public class O2oRecommendDao extends BaseO2oDao {
	private final Logger logger = LoggerFactory.getLogger(O2oRecommendDao.class);
	
	
	/**
	 * 客户推荐管理分页查询
	 * @param O2oZxVo
	 * @return
	 */
	public PageResult<Object[]> findByPage(O2oRecommendVo o2oRecommendVo) {
		SqlPageQuery<Object[]> pageQuery = new SqlPageQuery<Object[]>(Object[].class,o2oRecommendVo.getPageSize() ,
				o2oRecommendVo.getPageIndex() == null ? BaseUtils.PAGE_INDEX : o2oRecommendVo.getPageIndex());
		StringBuffer sql = new StringBuffer();
		sql.append("  select id,bill_code,user_name,"
				+ "CONCAT(LEFT (document_no, 3),'****',RIGHT (document_no, 3)) as document_no,"
				+ "CONCAT(LEFT (user_phone, 3),'****',RIGHT (user_phone, 3)) as user_phone,referrer_name, ");
		sql.append(" referrer_province_name,operator_name,operator_province_name,recommend_type,share_start_date,bill_status,is_type from o2o_recommend ");
		sql.append(" where  ");
		sql.append(" is_type !='D'  "); //(作废，和正常的都显示)
		if(!"1".equals(UserHolder.getUser().getOrg())){
			sql.append(" and creator_no= "+UserHolder.getUser().getNo());
		}
		
		if (StringUtil.hasText(o2oRecommendVo.getDocumentNo())) {
			sql.append(" and document_no = ?");
			pageQuery.addParam(o2oRecommendVo.getDocumentNo());
		}
		
		if (StringUtil.hasText(o2oRecommendVo.getUserName())) {
			sql.append(" and user_name like ?");
			pageQuery.addLikeParam(o2oRecommendVo.getUserName());
		}
		
		if (StringUtil.hasText(o2oRecommendVo.getOperatorName())) {
			sql.append(" and operator_name like ?");
			pageQuery.addLikeParam(o2oRecommendVo.getOperatorName());
		}
		if (StringUtil.hasText(o2oRecommendVo.getReferrerName())) {
			sql.append(" and referrer_name like ?");
			pageQuery.addLikeParam(o2oRecommendVo.getReferrerName());
		}
		if (StringUtil.hasText(o2oRecommendVo.getRecommendType())) {
			sql.append(" and recommend_type = ?");
			pageQuery.addParam(o2oRecommendVo.getRecommendType());
		}
		if (StringUtil.hasText(o2oRecommendVo.getBillStatus())) {
			sql.append(" and bill_status = ?");
			pageQuery.addParam(o2oRecommendVo.getBillStatus());
		}
		
		if (StringUtil.hasText(o2oRecommendVo.getCreatorTimeStart())) {
			sql.append(" and creator_time >= ? ");
			pageQuery.addParam(o2oRecommendVo.getCreatorTimeStart());
		}
		if (StringUtil.hasText(o2oRecommendVo.getCreatorTimeEnd())) {
			sql.append(" and creator_time <= ? ");
			GregorianCalendar gc = new GregorianCalendar();
			gc.setTime(DateUtils.formateStandDate(o2oRecommendVo.getCreatorTimeEnd()));
			gc.add(5, 1);
			pageQuery.addParam(gc.getTime());
		}
		
		if (StringUtil.hasText(o2oRecommendVo.getShareStartBDate())) {
			sql.append(" and share_start_date >= ? ");
			pageQuery.addParam(o2oRecommendVo.getShareStartBDate());
		}
		if (StringUtil.hasText(o2oRecommendVo.getShareStartFDate())) {
			sql.append(" and share_start_date <= ? ");
			GregorianCalendar gc = new GregorianCalendar();
			gc.setTime(DateUtils.formateStandDate(o2oRecommendVo.getShareStartFDate()));
			gc.add(5, 1);
			pageQuery.addParam(gc.getTime());
		}
		
		sql.append(" order by creator_time	desc ");
		//pageQuery.setSumColumnIndexs(new int[]{10});
		pageQuery.setQuerySql(sql.toString());
		logger.info("findByPage,sql={}", sql.toString());
		return this.findDataWithPage(pageQuery);
	}

	
}
